package com.chat.view;

import com.chat.Local;
import com.chat.service.*;
import com.chat.utils.Md5;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;

import javax.swing.*;
import java.util.Objects;

public class ChangePwdController {

    public TextField oldpwd;
    public TextField newpwd;
    public TextField confirmpwd;
    public Label oldpwdlabel;
    public Label newpwdlabel;
    public Label confirmpwdlabel;
    String Password;//保存加密之后的密码
    public void initialize(){
        System.out.println(Local.user.getMailbox());
    }
    public void change(){//修改按钮点击事件
          int flag=0;
            if(!Objects.equals(Local.pwd, oldpwd.getText())){
                     oldpwdlabel.setText("旧密码错误");
             }
        if(newpwd.getText().toString().matches("^[A-Za-z0-9]+${6,15}"))
        {    flag++;
            newpwdlabel.setText("格式正确！");
            Md5 k=new Md5();
            Password=k.digest(newpwd.getText().toString());
        }
        else newpwdlabel.setText("密码可以为字母\n和数字，数量在\n6-15之间！");

        if(newpwd.getText().toString().equals(confirmpwd.getText().toString())&&confirmpwd.getText().isEmpty()!=true){
            confirmpwdlabel.setText("格式正确!");
            flag++;

        }
        else if(confirmpwd.getText().isEmpty()==true){
            confirmpwdlabel.setText("格式错误！");
        }
        else confirmpwdlabel.setText("两次密码不一致！");
           if(flag==2)
           {
               System.out.println(Password);
               System.out.println(Local.user.getMailbox());
               ResetPwd.resetpwd(Local.user.getMailbox(),Password);
               JOptionPane.showMessageDialog(null, "修改成功！");
           }
    }
}
